package com.ym.medical.service.impl;

import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.CouponReceiveRecordDao;
import com.ym.medical.domain.bo.PublishCouponTarget;
import com.ym.medical.domain.entity.CouponEntity;
import com.ym.medical.domain.entity.CouponReceiveRecordEntity;
import com.ym.medical.enums.CouponUseStateEnum;
import com.ym.medical.enums.CouponValidModelEnum;
import com.ym.medical.service.ICouponReceiveRecordSV;
import com.ym.medical.service.ICouponSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: 张文博
 * @version: 2019年09月02日 9:47
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CouponReceiveRecordSVImpl extends BaseSVImpl<CouponReceiveRecordEntity,CouponReceiveRecordDao > implements ICouponReceiveRecordSV {

    @Autowired
    private ICouponSV couponSV;

    /**发放总结优惠劵
     * @param couponId 优惠劵id
     * @param createPerson  创建人
     * @param patientId 患者id
     * @param userId 用户id
     * @param sendWay 发送方式(key='CouponSendWayEnum')
     */
    @Override
    public List<Long> publishCoupon(Long couponId, String sendWay, Long businessId, Long createPerson,String remark, PublishCouponTarget...target) {
        List<Long> ids = new ArrayList<>();
        //优惠券信息 用来计算过期结束时间以及发放数量
        CouponEntity coupon = couponSV.selectById(couponId);
        int banlance = 0;
        //为零无限发放
        if (coupon.getNumLimit() != 0){
            banlance =  coupon.getDecNumLimit()- target.length;
            coupon.setDecNumLimit(banlance);
            couponSV.updateById(coupon);
            if (banlance<0){
                throw new BusinessException("优惠券不足");
            }
        }


        String type = coupon.getValidModel();
        Date invalidTime = null;
        //固定时间
        if (CouponValidModelEnum.FIXED_TIME.getKey().equals(type)){
            invalidTime = coupon.getEndTime();
            //动态时间
        }else if(CouponValidModelEnum.DYNAMIC_TIME.getKey().equals(type)){
            Date creTime = new Date();
            int validDay = coupon.getValidDay();
            invalidTime = new Date(TimeUnit.DAYS.toMillis(validDay) + creTime.getTime());
            //凌晨失效
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(invalidTime);
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.SECOND,0);
            invalidTime = calendar.getTime();

        }

        for (PublishCouponTarget publishCouponTarget : target) {
            CouponReceiveRecordEntity receiveRecord = new CouponReceiveRecordEntity();
            receiveRecord.setCouponId(couponId);
            receiveRecord.setPatientId(publishCouponTarget.getPatientId());
            receiveRecord.setUserId(publishCouponTarget.getUserId());
            receiveRecord.setSendWay(sendWay);
            receiveRecord.setRemark(remark);
            receiveRecord.setUserState(CouponUseStateEnum.DONT_USE.getKey());
            receiveRecord.setCreateTime(new Date());
            receiveRecord.setCreatePerson(createPerson);
            receiveRecord.setBusinessId(businessId);
            receiveRecord.setInvalidEndTime(invalidTime);
            this.insert(receiveRecord);
            ids.add( receiveRecord.getId());
        }
        return ids;
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        System.out.println(calendar.getTime());
        System.out.println(new Date(TimeUnit.DAYS.toMillis(7) + calendar.getTimeInMillis()));
        Long []d = {2L,3L,5L};
        System.out.println(Arrays.toString(d));

    }

    /**
     * 两周内随访的患者集合名
     * @param ids
     * @return
     */
    @Override
    public List<String> TowWeekRecord(Long[] ids) {
        List<String> names = mapper.selectTowWeekRecord(ids);
        return names;
    }
}
